package com.ruoyi.user.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.user.domain.vo.CartItemVO;
import com.ruoyi.user.domain.vo.CartSummaryVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.user.domain.UserShoppingCart;
import com.ruoyi.user.service.IUserShoppingCartService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户购物车Controller
 *
 * @author ruoyi
 * @date 2025-10-21
 */
@RestController
@RequestMapping("/user/cart")
public class UserShoppingCartController extends BaseController
{
    @Autowired
    private IUserShoppingCartService userShoppingCartService;

    /**
     * 查询用户购物车列表
     */
    @PreAuthorize("@ss.hasPermi('users:cart:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserShoppingCart userShoppingCart)
    {
        startPage();
        List<UserShoppingCart> list = userShoppingCartService.selectUserShoppingCartList(userShoppingCart);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('user:cart:list')")
    @GetMapping("/{customerId}/{merchantId}/items")
    public AjaxResult cartItems(@PathVariable("customerId") Long customerId,
                                @PathVariable("merchantId") Long merchantId) {
        List<CartItemVO> items = userShoppingCartService.listCartItems(customerId, merchantId);
        BigDecimal total = userShoppingCartService.getCartTotalPrice(customerId, merchantId);
        CartSummaryVO summary = new CartSummaryVO();
        summary.setItems(items);
        summary.setTotalPrice(total);
        return AjaxResult.success(summary);
    }

    /**
     * 导出用户购物车列表
     */
    @PreAuthorize("@ss.hasPermi('users:cart:export')")
    @Log(title = "用户购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserShoppingCart userShoppingCart)
    {
        List<UserShoppingCart> list = userShoppingCartService.selectUserShoppingCartList(userShoppingCart);
        ExcelUtil<UserShoppingCart> util = new ExcelUtil<UserShoppingCart>(UserShoppingCart.class);
        util.exportExcel(response, list, "用户购物车数据");
    }

    /**
     * 获取用户购物车详细信息
     */
    @PreAuthorize("@ss.hasPermi('users:cart:query')")
    @GetMapping(value = "/{cartId}")
    public AjaxResult getInfo(@PathVariable("cartId") String cartId)
    {
        return success(userShoppingCartService.selectUserShoppingCartByCartId(cartId));
    }

    /**
     * 新增用户购物车
     */
    @PreAuthorize("@ss.hasPermi('users:cart:add')")
    @Log(title = "用户购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserShoppingCart userShoppingCart)
    {
        return toAjax(userShoppingCartService.insertUserShoppingCart(userShoppingCart));
    }

    /**
     * 修改用户购物车
     */
    @PreAuthorize("@ss.hasPermi('users:cart:edit')")
    @Log(title = "用户购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserShoppingCart userShoppingCart)
    {
        return toAjax(userShoppingCartService.updateUserShoppingCart(userShoppingCart));
    }

    @PreAuthorize("@ss.hasPermi('user:cart:incre')")
    @PostMapping("/{customerId}/{merchantId}/{productId}/inc")
    public AjaxResult inc(@PathVariable("customerId") Long customerId,@PathVariable("merchantId") Long merchantId, @PathVariable("productId") Long productId) {
        userShoppingCartService.incrementItem(customerId,merchantId, productId);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('user:cart:decare')")
    @PostMapping("/{customerId}/{merchantId}/{productId}/dec")
    public AjaxResult dec(@PathVariable("customerId") Long customerId,@PathVariable("merchantId") Long merchantId,@PathVariable("productId") Long productId) {
        userShoppingCartService.decrementItemOrDelete(customerId,merchantId, productId);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('user:cart:add')")
    @PostMapping("/{customerId}/{merchantId}/product/{productId}/insert")
    public AjaxResult insertProduct(@PathVariable("customerId") Long customerId,
                                    @PathVariable("merchantId") Long merchantId,
                                    @PathVariable("productId") Long productId) {
        int rows = userShoppingCartService.insertCartItemByKeys(customerId, merchantId, productId);
        return AjaxResult.success(rows);
    }

    /**
     * 删除用户购物车
     */
    @PreAuthorize("@ss.hasPermi('user:cart:remove')")
    @Log(title = "用户购物车", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cartIds}")
    public AjaxResult remove(@PathVariable String[] cartIds)
    {
        return toAjax(userShoppingCartService.deleteUserShoppingCartByCartIds(cartIds));
    }
}
